#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QChartView>
#include <QLineSeries>
#include <QTimer>
#include <queue>
#include <QLabel>
#include <QScatterSeries>
#include "common_typedef.h"
#include "io_com.h"
#include "QLogTool.h"
#include "devaccuator.h"
#include "DeviceLinker.h"

namespace Ui {
class MainWindow;
}

using namespace QtCharts;
using namespace std;
using CURVE = vector<pair<size_t, double>>;

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    uint32_t Init();
    uint32_t InitChart();
    uint32_t InitChart(QLabel* lable, size_t curveIdx);
    void CfgChartStyle(QChart* pChart);
    void CfgChartRange(QChart* pChart, const QVariant &xMin, const QVariant &xMax,
                       const QVariant &yMin, const QVariant &yMax);
    /* 绘图资源 */
    QChart* m_chart {nullptr};
    QChartView* m_chartView {nullptr};
    QLineSeries* m_curve {nullptr};

    QChart* m_ptsChart[2] {nullptr};
    QChartView* m_ptsChartView[2] {nullptr};
    QScatterSeries* m_ptsCurve[2] {nullptr};
    CURVE m_data;
    CURVE m_ptsData[2];
    /* 设备管理 */
    shared_ptr<DevAccuator> m_dev {nullptr};
    unique_ptr<DeviceLinker> m_devLinker {nullptr};
    /* UI Timer */
    unique_ptr<QTimer> m_timr {nullptr};
signals:
    void SigGetDevDump();
    void SigSetCurrData(uint32_t ch, double val);
    void SigSetFixedData(uint32_t ch, double val, bool isMax);

private slots:
    void Log(QString info);
    void Update();

    void on_btOpenDev_clicked();
    void on_btReadPara_clicked();
    void on_btWritePara_clicked();

private:
    void UpdateSlvData();
    void UpdateSlvDataPara(size_t curveIdx);
    bool IsParaInput(QObject *watched) const;
    void ConfigSlvCalibPara(QObject *watched);

protected:
    bool eventFilter(QObject *watched, QEvent *event);
};

#endif // MAINWINDOW_H
